/**
 * @author yt
 * @description 统一返回结果
 */
class BaseResultCode {
  /***********************************/
  /**
   * code
   */
  code;
  /**
   * 说明
   */
  desc;

  constructor(code: number, desc: string) {
    this.code = code;
    this.desc = desc;
  }

  /************************************/
  static SUCCESS = new BaseResultCode(200, "成功");
  static FAILED = new BaseResultCode(500, "失败");
  static VALIDATE_FAILED = new BaseResultCode(400, "参数校验失败");
  static API_NOT_FOUNT = new BaseResultCode(404, "接口不存在");
  static API_BUSY = new BaseResultCode(429, "操作过于频繁");
  static TOKEN_OVERDUE = new BaseResultCode(-1, "Token无效，请重新登录");
}

export class BasicsResult {
  /**
   * 返回code
   */
  code;
  /**
   * 返回消息
   */
  msg;
  /**
   * 返回数据
   */
  data;
  /**
   * 返回时间
   */
  timestamp;

  /**
   * 是否成功
   */
  succeed;
  /**
   *
   * @param code {number} 返回code
   * @param msg {string} 返回消息
   * @param data {any} 返回具体对象
   */
  constructor(code: number, msg: string, data: any, succeed: boolean) {
    this.code = code;
    this.msg = msg;
    this.data = data;
    this.succeed = succeed;
    this.timestamp = Date.now();
  }

  /**
   * 成功
   * @param data {any} 返回对象
   * @return {Result}
   */
  static success(data: any) {
    return new BasicsResult(
      BaseResultCode.SUCCESS.code,
      BaseResultCode.SUCCESS.desc,
      data,
      true
    );
  }

  /**
   * 失败
   */
  static error(code: number, msg: string) {
    return new BasicsResult(code, msg, null, false);
  }

  /**
   * 失败
   */
  static fail(errData: number) {
    return new BasicsResult(
      BaseResultCode.FAILED.code,
      BaseResultCode.FAILED.desc,
      errData,
      false
    );
  }

  /**
   * token过期
   */
  static tokenOverdue() {
    return new BasicsResult(
      BaseResultCode.TOKEN_OVERDUE.code,
      BaseResultCode.TOKEN_OVERDUE.desc,
      null,
      false
    );
  }

  /**
   * 参数校验失败
   */
  static validateFailed(param: any) {
    return new BasicsResult(
      BaseResultCode.VALIDATE_FAILED.code,
      BaseResultCode.VALIDATE_FAILED.desc,
      param,
      false
    );
  }

  /**
   * 拦截到的业务异常
   * @param bizException {BizException} 业务异常
   */
  static bizFail(bizException: any) {
    return new BasicsResult(bizException.code, bizException.msg, null, false);
  }
}
